package org.testis.gui.mvc.model.testdesign;

import org.testis.gui.mvc.model.*;
import org.testis.gui.mvc.controller.testdesign.TreeController;
import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.util.LinkedList;
import javax.swing.tree.TreePath;

/**
 *
 * @author Cahit Emir Erel
 */
public class TreeModel extends AbstractModel {

    private String m_properties = "";
    private MyDefaultMutableTreeNode m_selectedNode = null;
    private LinkedList<MyDefaultMutableTreeNode> m_loadedTreeList =
            new LinkedList<MyDefaultMutableTreeNode>();
    private MyDefaultMutableTreeNode m_source = null;
    private MyDefaultMutableTreeNode m_destination = null;
    private Boolean m_compareMode = false;
    private LinkedList<TreePath> currentTreePaths =
            new LinkedList<TreePath>();

    public TreeModel() {
    }

    public void setCompareMode(Boolean mode) {
        Boolean oldMode = m_compareMode;
        m_compareMode = mode;
        if (mode != null) {
            firePropertyChange(TreeController.COMPARE_MODE_ON,
                    oldMode,
                    m_compareMode);
        }
    }

    public void setReloadTree(Boolean reload) {
        if (reload != null && reload) {
            firePropertyChange(TreeController.TREE_RELOAD,
                    null,
                    reload);
        }
    }

    public void setShowTab(Integer tab) {
        if (tab != null && tab >= 0 && tab < 2) {
            firePropertyChange(TreeController.SHOW_TAB,
                    null,
                    tab);
        }
    }

    public void setRefreshTree(Boolean refresh) {
        if (refresh != null && refresh) {
            firePropertyChange(TreeController.TREE_REFRESH,
                    false,
                    true);
        }
    }

    public void setHideTab(Integer tab) {
        if (tab != null && tab >= 0 && tab < 2) {
            firePropertyChange(TreeController.HIDE_TAB,
                    null,
                    tab);
        }
    }

    /**
     * Provides the means to set or reset the model to a default state.
     */
    public void initDefault() {
        setProperties("Welcome to ATOF Test Designer");
    }

    //  Accessors
    public void setSaveTree(MyDefaultMutableTreeNode tree) {
        if (tree != null) {
            firePropertyChange(TreeController.TREE_SAVE,
                    null,
                    tree);
        }
    }

    public void setTreePath(TreePath treePath) {
        if (treePath == null) {
            return;
        }
        String rootName = treePath.getPath()[1].toString();
        for (int i = 0; i < currentTreePaths.size(); i++) {
            TreePath path = currentTreePaths.get(i);
            String rootNameForPath = path.getPath()[1].toString();
            if (rootName.equals(rootNameForPath)) {
                currentTreePaths.remove(i);
                break;
            }
        }
        currentTreePaths.add(treePath);
        firePropertyChange(TreeController.SAVE_TREE_PATH,
                null,
                treePath);
    }

    public LinkedList<TreePath> getTreePath() {

        return currentTreePaths;
    }

    public void setLoadTree(MyDefaultMutableTreeNode tree) {
        if (tree == null) {
            return;
        }
        m_loadedTreeList.add(tree);
        firePropertyChange(TreeController.TREE_LOAD,
                null,
                tree);
    }

    public void setReloadTreePath(TreePath path) {
        if (path == null) {
            return;
        }
        firePropertyChange(TreeController.RELOAD_TREE_PATH,
                null,
                path);
    }

    public LinkedList<MyDefaultMutableTreeNode> getLoadTree() {
        return m_loadedTreeList;
    }

    public MyDefaultMutableTreeNode getSelectedNode() {
        return m_selectedNode;
    }

    public void setSelectedNode(MyDefaultMutableTreeNode selectedNode) {
        MyDefaultMutableTreeNode oldSelectedNode = m_selectedNode;
        m_selectedNode = selectedNode;
        firePropertyChange(TreeController.SELECT_DESIGN_PART,
                oldSelectedNode,
                selectedNode);
    }

    public String getProperties() {
        return m_properties;
    }

    public void setProperties(String properties) {
        if (properties == null) {
            properties = "";
        }
        String oldProperties = m_properties;
        m_properties = properties;
        firePropertyChange(TreeController.SHOW_PROPERTIES,
                oldProperties,
                properties);
    }

    public void setDelete(Boolean delete) {
        if (delete != null && delete) {
            firePropertyChange(TreeController.NODE_DELETE,
                    false,
                    true);
        }
    }

    public MyDefaultMutableTreeNode getSource() {
        return m_source;
    }

    public void setSource(MyDefaultMutableTreeNode source) {
        MyDefaultMutableTreeNode oldSource = m_source;
        m_source = source;
        firePropertyChange(TreeController.SOURCE,
                oldSource,
                source);
    }

    public MyDefaultMutableTreeNode getDestination() {
        return m_destination;
    }

    public void setDestination(MyDefaultMutableTreeNode destination) {
        MyDefaultMutableTreeNode oldDestination = m_destination;
        m_destination = destination;
        firePropertyChange(TreeController.DESTINATION,
                oldDestination,
                destination);
    }

    public void setClearSelection(Boolean value) {
        if (value != null && value) {
            firePropertyChange(TreeController.CLEAR_SELECTION_PROPERTY,
                    null,
                    value);
        }
    }
}
